package com.stu.manager.course.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.manager.config.Result;
import com.stu.manager.course.entity.SysCourse;
import com.stu.manager.course.entity.dto.CourseDto;
import com.stu.manager.course.mapper.SysCourseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.sql.Time;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-02-15
 */
@Service
public class SysCourseServiceImpl extends ServiceImpl<SysCourseMapper, SysCourse> {

    public IPage<SysCourse> pageCourse(Page<SysCourse> pageCourse, QueryWrapper<SysCourse> wrapper) {
        IPage<SysCourse> sysCourseIPage = baseMapper.selectPage(pageCourse, wrapper);
        return sysCourseIPage;
    }

    //添加课程
    public Result addCourse(CourseDto courseDto) {
        //先看同名\同专业的有没有
        String courseName = courseDto.getCourseName();
        String professionalName = courseDto.getProfessionalName();
        Integer classGrade = courseDto.getClassGrade();
        QueryWrapper<SysCourse> wrapperName = new QueryWrapper<>();
        wrapperName.eq("course_name",courseName).eq("professional_name",professionalName).eq("class_grade",classGrade);
        Integer count = baseMapper.selectCount(wrapperName);
        if(count>0){
            return Result.error().message("该专业此课程已添加！");
        }

        SysCourse sysCourse = new SysCourse();
        BeanUtils.copyProperties(courseDto,sysCourse);
        int insert = baseMapper.insert(sysCourse);
        if(insert>0){
            return Result.ok().message("添加成功！");
        }else{
            return Result.error().message("添加失败！");
        }
    }

    //修改前查询单个课程信息
    public SysCourse getCourseById(String id) {
        QueryWrapper<SysCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        SysCourse sysCourse = baseMapper.selectOne(wrapper);
        return sysCourse;
    }

    //修改课程
    public Result updateCourse(SysCourse sysCourse) {
        Integer classGrade = sysCourse.getClassGrade();
        String courseName = sysCourse.getCourseName();
        String departmentName = sysCourse.getDepartmentName();
        String professionalName = sysCourse.getProfessionalName();
        String id = sysCourse.getId();

        //判断修改的课程是否已存在
        QueryWrapper<SysCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("course_name",courseName).eq("class_grade",classGrade).eq("professional_name",professionalName).ne("id",id);
        Integer count = baseMapper.selectCount(wrapper);
        if(count>0){
            return Result.error().message("该专业此课程已添加！");
        }

        int i = baseMapper.updateById(sysCourse);
        if(i>0){
            return Result.ok().message("修改成功！");
        }else{
            return Result.error().message("修改失败！");
        }

    }

    //修改课程教师时间信息
    public Result updateCourseManage(SysCourse sysCourse) {
        String courseTime = sysCourse.getCourseTime();
        String roomAddr = sysCourse.getRoomAddr();//上课地址
        long jobNum = sysCourse.getJobNum();
        String courseWeek = sysCourse.getCourseWeek();
        String id = sysCourse.getId();

        //判断同一时间统一老师存在那么这节课不能安排
        QueryWrapper<SysCourse> wrapperTeacher = new QueryWrapper<>();
        wrapperTeacher.eq("course_week",courseWeek).eq("course_time",courseTime).eq("job_num",jobNum).ne("id",id);
        Integer count = baseMapper.selectCount(wrapperTeacher);
        if(count>0){
            return Result.error().message("当前课程安排有冲突,请调整！");
        }

        //同一时间同一教室
        QueryWrapper<SysCourse> wrapperRoom = new QueryWrapper<>();
        wrapperRoom.eq("room_addr",roomAddr).eq("course_week",courseWeek).eq("course_time",courseTime).ne("id",id);
        Integer count1 = baseMapper.selectCount(wrapperRoom);
        if(count1>0){
            return Result.error().message("当前课程安排地址时间有冲突,请调整！");
        }

        int i = baseMapper.updateById(sysCourse);
        if(i>0){
            return Result.ok().message("修改成功！");
        }else {
            return Result.error().message("修改失败！");
        }
    }

    //findCoursePlan
    public List<SysCourse> findCoursePlan(String jobNum) {
        QueryWrapper<SysCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("job_num",jobNum);
        List<SysCourse> sysCourses = baseMapper.selectList(wrapper);
        return sysCourses;
    }
}
